﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel;
using System.IO;
using System.Data;
using System.Drawing;

namespace FrontEnd_step2
{
    class ModdbFrontEnd
    {
        Moddb08_WCF.Itest client;
        
        public ModdbFrontEnd()
        {
            WebChannelFactory<Moddb08_WCF.Itest> factory = new WebChannelFactory<Moddb08_WCF.Itest>(new Uri("http://localhost:9998"));
            client = factory.CreateChannel();
           
        }

        public Moddb08_WCF.Tables GetAllTables()
        {
            Moddb08_WCF.Tables tables = client.GetAllTables();
            return tables;
        }

        public Moddb08_WCF.Columns GetAllColumns(string tableName)
        {
            return client.GetAllColumnsOfTable(tableName);
        }

        public Moddb08_WCF.Rows GetAllRows(string tableName)
        {
            return client.GetAllRows(tableName);
        }

        public bool CreateTable(string tableName)
        {
            client.AddNewTable(new Moddb08_WCF.Table(tableName));
            return true;

        }

        public Stream GetColumnData(string tableName, string rowID, string columnName)
        {
            return client.GetColumnData(tableName, rowID, columnName);
        }

        public DataTable GetDataTable(string table)
        {
            DataTable dataTable = new DataTable(table);
            Moddb08_WCF.Columns columns = GetAllColumns(table);
            foreach (Moddb08_WCF.Column col in columns)
            {
                
                dataTable.Columns.Add(new DataColumn(col.Name, ParseDataType(col.Type));
                if(col.Name.ToUpper().Equals("ID"))
                {
                    dataTable.Columns[col.Name].AutoIncrement = true;
                    dataTable.Columns[col.Name].ReadOnly = true;
                }
            }

            return dataTable;

        }




        # region Private Methods

        /// <summary>
        /// Get the datatype of the data
        /// </summary>
        /// <param name="dataType">The name of the datatype</param>
        /// <returns>The datatype object of the data</returns>
        private Type ParseDataType(string dataType)
        {
            if (dataType.ToUpper().Equals("INT"))
            {
                return typeof(int);
            }
            else if (dataType.ToUpper().Equals("SMALLINT"))
            {
                return typeof(Int16);
            }
            else if (dataType.ToUpper().Equals("DECIMAL"))
            {
                return typeof(decimal);
            }
            else if (dataType.ToUpper().Equals("NCHAR") || dataType.ToUpper().Equals("VARCHAR") ||
                        dataType.ToUpper().Equals("TEXT"))
            {
                return typeof(string);
            }
            else if (dataType.ToUpper().Equals("IMAGE"))
            {
                return typeof(byte[]);
            }
            else if (dataType.ToUpper().Equals("MONEY"))
            {
                return typeof(Decimal);
            }
            else if (dataType.ToUpper().Equals("DATE") || dataType.ToUpper().Equals("DATETIME"))
            {
                return typeof(DateTime);
            }
            else
            {
                return null;
            }

        #endregion
        }
    }
}
